﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using BaseHelper.Encrypt;

namespace BaseHelper.Text
{
    public class TextFontSizeHelper
    {
        public static int CalcFontSize(string str, int Width, int Height, float fixSize = 1.1f)
        {
            return Width / Height > 3 ? CalcFontSizeByM(str, Width, Height) : CalcFontSizeByF(str,  Width, Height, fixSize);
        }

        private static int CalcFontSizeByF(string str, int Width, int Height, float fixSize = 1.1f)
        {
            //字体放大系数，会自动将计算出的字体x系数。
            //因为普通的控制卡，会有字符间距，导致字体实际占用要大一点。
            //高速公路的话，这里可能是1f就可以了。

            int FontSize = 0;
            if (Height == 0 || Width == 0)
                return 16;

            try
            {
                if (str.Contains("\n"))
                {
                    int maxSize = 0;
                    int minSize = str.Length;

                    string[] lines = str.Split('\n');
                    foreach (string l in lines)
                    {
                        if (l.Length > maxSize)
                            maxSize = l.Length;
                        else if (l.Length < minSize)
                            minSize = l.Length;
                    }


                    if (maxSize <= 10)
                    {
                        int YCount = (int)Math.Floor(Height / (lines.Length * fixSize));
                        int XCount = (int)Math.Floor((double)(Width / (maxSize * fixSize)));

                        if (YCount % 2 == 1)
                            YCount--;

                        if (XCount % 2 == 1)
                            XCount--;

                        if (XCount < YCount)
                            FontSize = XCount;
                        else
                            FontSize = YCount;

                        if (FontSize < 16)
                            FontSize = 16;
                    }

                    if (lines.Length == 2)
                    {
                        str = str.Replace("\n", "\n\n");
                    }

                }

                if (FontSize == 0)
                {
                    FontSize = (int)Math.Sqrt(Width * Height / str.Length);
                    if (FontSize % 2 == 1)
                        FontSize--;

                    if (FontSize > 0)
                    {
                        do
                        {
                            int XCount = (int)Math.Floor((double)(Width / (FontSize * fixSize)));
                            int YCount = (int)Math.Floor((Height / (FontSize * fixSize)));
                            if (XCount * YCount >= str.Length)      //>修改为>=
                            {
                                break;
                            }
                            else
                                FontSize -= 2;

                        } while (true);
                    }

                    if (FontSize < 16)//18
                        FontSize = 16;//18
                }
            }
            catch (Exception ex)
            {
                LogHelper.Err("BaseHelper.Text.TextFontSizeHelper.cs", "CalcFontSizeByF(" + str+ "," + Width.ToString() +","+ Height.ToString()+")", ex);
            }

            return FontSize;
        }

        public static int CalcFontSizeByM(string str, int Width, int Height)
        {
            //判断是否是F架，因为F架一般屏幕比1:2，长是宽2倍，这里判断小于比例3就是F架
            bool IsFMode = ((float)Width / (float)Height) <= 3.0;

            int FontSize = 0;

            try
            {
                //如果是F架诱导屏
                if (IsFMode)
                {
                    //调用F架计算方法，计算出来的结果肯定比较小
                    //因为，F架计算的时候普通的控制卡会显示字体偏大，我进行了缩小。
                    //高速公路的工控机不用缩小，这里可能需要
                    //1.0f代表的缩放修正系数，就是1：1的字体大小

                    FontSize = CalcFontSizeByF(str, Height, Width, 1.0f);
                }
                else
                {
                    //如果是龙门架的话，用宽/字数，得出平均每个字符宽度，然后判断小于屏幕高度才可以。
                    FontSize = Width / str.Length;
                    if (FontSize > Height)
                        FontSize = Height;
                }


                int[] SizeList = new int[] { 64, 48, 32, 24, 16 };
                for (int i = 0; i < SizeList.Length; i++)
                {
                    if (FontSize >= SizeList[i])
                        return SizeList[i];
                }
            }
            catch (Exception ex)
            {
                LogHelper.Err("BaseHelper.Text.TextFontSizeHelper.cs", "CalcFontSizeByM(" + str + "," + Width.ToString() + "," + Height.ToString() + ")", ex);
            }
            return FontSize;
        }

        public static bool IsFMode(string Width, string Height)
        {
            return (Convert.ToSingle(Width) / Convert.ToSingle(Height)) <= 3.0;
        }

        public static int Calc2(string str, int Height, int Width)
        {
            int FontSize = 0;
            float fixSize = 1.1f;
            int PositionHor = 0;

            if (str.Contains("\n"))
            {

                if (PositionHor == -1)
                    PositionHor = 2;

                int maxSize = 0;
                int minSize = str.Length;

                string[] lines = str.Split('\n');
                foreach (string l in lines)
                {
                    if (l.Length > maxSize)
                        maxSize = l.Length;
                    else if (l.Length < minSize)
                        minSize = l.Length;
                }

                if (maxSize <= 10)
                {
                    int YCount = (int)Math.Floor(Height / (lines.Length * fixSize));
                    int XCount = (int)Math.Floor((double)(Width / (maxSize * fixSize)));

                    if (YCount % 2 == 1)
                        YCount--;

                    if (XCount % 2 == 1)
                        XCount--;

                    if (XCount < YCount)
                        FontSize = XCount;
                    else
                        FontSize = YCount;

                    if (FontSize < 18)
                        FontSize = 18;
                }
            }

            if (FontSize == 0)
            {
                FontSize = (int)Math.Sqrt(Width * Height / str.Length);
                if (FontSize % 2 == 1)
                    FontSize--;

                do
                {
                    int XCount = (int)Math.Floor((double)(Width / (FontSize * fixSize)));
                    int YCount = (int)Math.Floor((Height / (FontSize * fixSize)));
                    if (XCount * YCount > str.Length)
                    {
                        break;
                    }
                    else
                        FontSize -= 2;

                } while (true);


                if (FontSize < 18)
                    FontSize = 18;


                if (PositionHor == -1)
                {
                    if (FontSize * str.Length <= Width)
                        PositionHor = 2;
                    else
                        PositionHor = 0;
                }
            }
            return FontSize;


        }
    }
}
